﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Timers;

public partial class Main_Personal_Reminders : System.Web.UI.Page
{
    /*page defaults*/
    OfficeOnlineUser currentUser;
    

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/

    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();

        txtCurrentUser.Value = currentUser.UserName;
        
        if (Request.QueryString["updatereminder"] == "1")
        {
            int result = UpdateReminder();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in updating the reminder, please contact the system admin." + "</response>";
                    break;
            }
        }
        else if (Request.QueryString["insertreminder"] == "1")
        {
            int result = InsertReminder();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in adding the reminder, please contact the system admin." + "</response>";
                    break;
            }
        }
        else if (Request.QueryString["deletereminder"] == "1")
        {
            int result = DeleteReminder();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in deleting the reminder, please contact the system admin." + "</response>";
                    break;
            }
        }
        else
        {
            BindRemindersTable();                 
        }
    }    

    private void BindRemindersTable()
    {
        Reminder rmdr = new Reminder();
        rmdr.Username = currentUser.UserName;
        DataTable dsReminders = rmdr.SelectAllByUsername();

        GridView1.DataSource = dsReminders;
        GridView1.DataBind();
    }

    protected void GridView1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                e.Item.Attributes.Add("onclick", "selectReminderTableElement('" + e.Item.ClientID + "')");
                e.Item.Attributes.Add("reminderID", DataBinder.Eval(e.Item.DataItem, "reminderID").ToString());
                e.Item.Cells[0].Style.Add("padding", "2px 0 2px 2%");
                e.Item.Cells[1].Style.Add("padding", "2px 0 2px 2%");
                //e.Item.Cells[1].ForeColor = System.Drawing.Color.Blue;
                //e.Item.Cells[1].Style.Add("text-decoration", "underline");
                //e.Item.Cells[1].Style.Add("cursor", "pointer");
                e.Item.Cells[1].Attributes.Add("title", DataBinder.Eval(e.Item.DataItem, "title").ToString());
                e.Item.Cells[2].Attributes.Add("title", DataBinder.Eval(e.Item.DataItem, "description").ToString());
                break;
        }
    }

    private int DeleteReminder()
    {
        string reminderID = Request.QueryString["reminderID"];

        Reminder rmdr = new Reminder();

        rmdr.ID = reminderID;

        return rmdr.Delete();
    }

    private int UpdateReminder()
    {
        string reminderID = Request.QueryString["reminderID"];
        string title = Request.QueryString["title"];
        string description = Request.QueryString["desc"];
        string alarmDate = Request.QueryString["alarmDate"];

        Reminder rmdr = new Reminder();

        rmdr.ID = reminderID;
        rmdr.Title = title;
        rmdr.Description = description;
        rmdr.AlarmTime = alarmDate;

        return rmdr.Update();
    }

    private int InsertReminder()
    {
        string username = Request.QueryString["username"];
        string title = Request.QueryString["title"];
        string description = Request.QueryString["desc"];
        string alarmDate = Request.QueryString["alarmDate"];

        Reminder rmdr = new Reminder();

        rmdr.Username = username;
        rmdr.Title = title;
        rmdr.Description = description;
        rmdr.AlarmTime = alarmDate;

        return rmdr.Insert();
    }
}
